import View from "../../../../mvc/View";
import Commands from "../constants/Commands";
import {gContext} from "./vc/Main";

export default class SystemLoadingView extends View {

    constructor(name, viewComponent) {
        super(name, viewComponent);
    }

    getSystemLoadingVc() {
        return this.viewComponent;
    }

    onRegister() {
        let vc = this.getSystemLoadingVc();
        vc.visible = false;
        gContext.app.stage.addChild(vc);
        gContext.app.ticker.add(this.advanceTime, this);
    }

    onRemove() {
        gContext.app.ticker.remove(this.advanceTime, this);
        let vc = this.getSystemLoadingVc();
        vc.destroy(true);
    }

    listEventInterests() {
        return [Commands.SYS_LOADING_PROGRESS, Commands.LOADING_COMPLETE];
    }

    handleEvent(type, data, sponsor) {
        let vc = this.getSystemLoadingVc();
        switch (type) {
            case Commands.SYS_LOADING_PROGRESS:
                vc.visible = true;
                break;
            case Commands.LOADING_COMPLETE:
                vc.visible = false;
                break;
            default:
        }
    }

    advanceTime() {
        let vc = this.getSystemLoadingVc();
        if (vc.visible) {
            vc.progressBar.rotation += 0.1;

            let num = Math.round(vc.progressBar.rotation) % 4;
            if (num === 0) {
                vc.loadingPointText0.visible = false;
                vc.loadingPointText1.visible = false;
                vc.loadingPointText2.visible = false;
            } else if (num === 1) {
                vc.loadingPointText0.visible = true;
                vc.loadingPointText1.visible = false;
                vc.loadingPointText2.visible = false;
            } else if (num === 2) {
                vc.loadingPointText0.visible = true;
                vc.loadingPointText1.visible = true;
                vc.loadingPointText2.visible = false;
            } else {
                vc.loadingPointText0.visible = true;
                vc.loadingPointText1.visible = true;
                vc.loadingPointText2.visible = true;
            }
        }

    }
}